const request = (url, config) => {
    let reqUrl = url;
    if (!reqUrl.startsWith("http")) {
        reqUrl = import.meta.env.ASTRO_PUBLIC_API_BASE || 'https://api-test.bgshfamily.com' + url;
        console.log('reqUrl:', reqUrl)
    }
    return fetch(reqUrl, config)
        .then(response => {
            if (!response.ok) {
                throw new Error(`HTTP error! Status: ${response.status}`);
            }
            return response;
        })
        .then(response => {
            if (response.headers.get('Content-Type').includes('application/json')) {
                return response.json();
            }
            return response.text();
        })
        .catch(error => {
            console.error('Error during fetch:', error);
            throw error;
        });
};

// GET请求
export const get = (url) => {
    return request(url, { method: 'GET' });
};

// POST请求
export const post = (url, data) => {
    return request(url, {
        body: JSON.stringify(data),
        headers: {
            'content-type': 'application/json'
        },
        method: 'POST'
    });
};